/** 登录 v1.0.0 **/
//渲染登录URL语言
kaLanguage.renderingOption("languageSelect", function () { });
// 手机字体点击验证码
var sliderVerifyBoolean = false;
// 当前登入模式
var loginmode = 0;
var login = {
    verificationImgBoolean: false,
    init: function () { //初始化
        if(!kaValue.isBackend){ // 判断是否后端
            $("#code").val("12");
        }
        //登录成功后，跳转首页页面
        var kaindexpage = $.getUrlParam('indexpage');
        if (kaindexpage != undefined && kaindexpage != null && kaindexpage != "") {
            sessionStorage.setItem("indexpage", kaindexpage);
        } else {
            sessionStorage.setItem("indexpage", "");
        }
        //判断是否手机端
        var tabsStr = kaValue.login.tabsStr; //["账号登录", "手机登录", "扫码登录"]
        var tabsheader = []; // 登录页签
        var tabsbody = []; // 登录页签内容
        var appPhone = ka.isAppPhoneOrExe();
        if (appPhone.code == "200" && appPhone.type == "APP") { //手机端
            tabsheader.push({ title: tabsStr[0] });//账号登录
            tabsheader.push({ title: tabsStr[1] });//手机登录
            tabsbody.push({ content: login.getAccountLoginHtml() });
            tabsbody.push({ content: login.getPhoneLoginHtml() });
            $("#registerAccountDiv").hide();
        } else { //PC端 EXE
            tabsheader.push({ title: tabsStr[0] });//账号登录
            tabsheader.push({ title: tabsStr[1] });//手机登录
            tabsheader.push({ title: tabsStr[2] });//扫码登录
            tabsbody.push({ content: login.getAccountLoginHtml() });
            tabsbody.push({ content: login.getPhoneLoginHtml() });
            tabsbody.push({ content: login.getLoginQrcodeHtml() });
            $("#registerAccountDiv").show();
            if (appPhone.type == "EXE") { //启动
            }
        }
        //渲染登录页面
        layui.tabs.render({
            elem: '#loginmodeDiv',
            header: tabsheader,
            body: tabsbody,
            index: loginmode, // 初始选中标签索引
            className: 'layui-tabs-card'
        })
        // tab 切换事件
        layui.tabs.on('afterChange(loginmodeDiv)', function (data) {
            loginmode = data.index;
            if (loginmode == "2") { // 扫码登录
                login.getLoginQrcode();
                $("#verificationImgDiv").hide();
                $("#automaticLogonDivAndForgotPasswordS").hide();
                $("#loginLaySubmit").addClass("layui-btn-disabled");
                $("#loginLaySubmit").prop('disabled', true); // 禁用按钮
            } else if (loginmode == "1") { // 手机号码登录
                $("#verificationImgDiv").hide();
                $("#automaticLogonDivAndForgotPasswordS").show();
                $("#loginLaySubmit").removeClass("layui-btn-disabled");
                $("#loginLaySubmit").removeAttr('disabled');
            } else { // 账号密码登录
                $("#verificationImgDiv").show();
                $("#automaticLogonDivAndForgotPasswordS").show();
                $("#loginLaySubmit").removeClass("layui-btn-disabled");
                $("#loginLaySubmit").removeAttr('disabled');
            }
            layer.msg('<span>已切换成</span>' + "“<span style=\"color: #ffd600;font-weight: 900;\">" + (loginmode == 0 ? kaValue.login.tabsStr[0] : loginmode == 1 ? kaValue.login.tabsStr[1] : loginmode == 2 ? kaValue.login.tabsStr[2] : kaValue.login.tabsStr[0]) + "</span>”");
        });
        // 判断是否记住密码
        var ka_automatic_logon = kaValue.automaticLogon();
        if (ka_automatic_logon != undefined && ka_automatic_logon != null && ka_automatic_logon != "") {
            var ka_automatic_logonJSON = JSON.parse(ka_automatic_logon);
            ka_automatic_logonJSON.code = "";
            loginmode = ka_automatic_logonJSON.loginmode;
            layui.form.val("ka-form-login", ka_automatic_logonJSON);
            layui.tabs.change('loginmodeDiv', loginmode);
        }
        //绑定
        $("#kaFormLogin").keydown(function(event){
            if(event.keyCode == 13){
                $("#loginLaySubmit").click();
                return false; 
            }
        });
    },
    getAccountLoginHtml: function () { // 获取账号登录Html
        var accountLoginHtml = `
            <!-- 登录内容 - 账号登陆 - 账号 -->
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input id="username" name="username" placeholder="账户" type="text" autocomplete="off" lay-affix="clear" class="layui-input">
                </div>
            </div>
            <!-- 登录内容 - 账号登陆 - 密码 -->
            <div class="layui-form-item" style="margin-bottom: 0px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input id="password" name="password" placeholder="密码" type="password" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
        `;
        return accountLoginHtml;
    },
    getPhoneLoginHtml: function () { // 手机登录
        var phoneLoginHtml = `
            <!-- 登录内容 - 手机登录 - 手机号码  -->
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-cellphone"></i>
                    </div>
                    <input id="phonenumber" name="phonenumber" lay-vertype="tips" placeholder="手机号码" type="text" autocomplete="off" lay-affix="clear" class="layui-input">
                </div>
            </div>
            <!-- 登录内容 - 手机登录 - 手机验证码  -->
            <div class="layui-form-item" style="margin-bottom: 0px;">
                <div class="layui-row">
                    <div class="layui-col-xs9">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-auz"></i>
                            </div>
                            <input id="codePhonenumber" name="codePhonenumber" lay-vertype="tips" placeholder="手机验证码" type="text" autocomplete="off" lay-affix="clear" class="layui-input">
                        </div>
                    </div>
                    <!-- 登录内容 - 手机登录 - 手机验证码 - 获取验证码  -->
                    <div class="layui-col-xs3">
                        <div style="margin-left: 11px;">
                            <button type="button" id="PhonenVerCodeButton" onclick="login.getPhonenVerCode()" style="background: unset;color: var(--global-primary-color)!important;" class="layui-btn layui-btn-fluid layui-btn-primary layui-border-blue">获取</button>
                        </div>
                    </div>
                </div>
            </div>
        `;
        return phoneLoginHtml;
    },
    getLoginQrcodeHtml: function () { //扫码登录
        var loginQrcodeHtml = `<div id="loginQrcode" style="text-align: center;"></div>`;
        return loginQrcodeHtml;
    },
    getConfig: function (code, callback) { //获取配置
        var option = kaValue.getUrl(kaValue.login.url.getConfig);
        option.url = option.url + "/" + code;
        option.success = function (re) {
            if (re.code == 200) {
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            }
        }
        ka.ajax(option);
    },
    captchaImage: function () { //验证码
        var captchaImageOption = kaValue.getUrl(kaValue.login.url.captchaImage);
        captchaImageOption.success = function (re) {
            if (re.code == 200) {
                var image = "data:image/gif;base64," + re.img;
                $("#verificationImg").attr("src", image);
                $("#verificationImgDiv").css("display", "flex");
                verificationImgBoolean = true;
                $("#uuid").val(re.uuid);
                if (loginmode == 1 || loginmode == 2) {
                    $("#verificationImgDiv").hide();
                }
            }
        }
        ka.ajax(captchaImageOption);
    },
    loginTenant: function (callback) { //获取租户
        var loginTenantOption = kaValue.getUrl(kaValue.login.url.loginTenant);
        loginTenantOption.success = function (re) {
            if (re.code == 200) {
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            }
        }
        ka.ajax(loginTenantOption);
    },
    register: function () { //注册
    },
    forgotpassword: function () { //忘记密码
    },
    login: function (field, callback) { //登录
        var option = kaValue.getUrl(kaValue.login.url.login);
        option.data = field;
        option.url = option.url;
        option.success = function (re) {
            if (re.code == 200) {
                kaMsg.success(re);
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            } else {
                login.captchaImage();
                kaMsg.error(re);
            }
        }
        ka.ajax(option);
    },
    setAutomaticLogon: function (field) { //设置登录信息
        if (field.loginmode != "1") {
            field.username = "";
            field.password = "";
            field.codePhonenumber = "";
        }
        field.code = "";
        localStorage.setItem("ka_automatic_logon", JSON.stringify(field));
    },
    sliderVerify: function () { // 打开点击验证码
        if (!sliderVerifyBoolean) {
            var sliderVerifyImgName = [];
            var option = kaValue.getUrl(kaValue.login.url.commonAdGetSliderVerify);
            option.async = false;
            option.success = function (re) {
                if (re.code == 200) {
                    sliderVerifyImgName = re.data;
                }
            }
            ka.ajax(option);
            var layerIndex = layer.open({
                type: 1,
                area: ['304px', '376px'], // 宽高
                title: false, // 不显示标题栏
                closeBtn: 0,
                shadeClose: false, // 点击遮罩关闭层
                shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
                content: `<div id="sliderVerify"></div>`,
                success: function (layero, index, that) {
                    $('#sliderVerify').pointsVerify({
                        defaultNum: 5, //默认的文字数量
                        checkNum: 4, //校对的文字数量
                        vSpace: 4, //间隔
                        imgName: sliderVerifyImgName,
                        imgSize: {
                            width: '280px',
                            height: '290px',
                        },
                        barSize: {
                            width: '90%',
                            height: '40px',
                        },
                        ready: function (e) {
                        },
                        success: function (e) {
                            sliderVerifyBoolean = true;
                            layui.layer.closeAll();
                            login.getPhonenVerCode();
                        },
                        error: function () {
                            sliderVerifyBoolean = false;
                            kalog.outLogs(["sliderVerify生成错误", {}]);
                        }
                    });
                    $(".verify-close").click(function () {
                        layui.layer.close(layerIndex);
                    });
                }
            });
        }
    },
    getPhonenVerCode: function () { //获取手机验证码
        if (loginmode == 1) {
            var phonenumber = $("#phonenumber").val();
            if (phonenumber != undefined && phonenumber != null && phonenumber != "") {
                var regMobile = kaValue.regMobile;
                if (!regMobile.test(phonenumber)) {
                    var option = {
                        code: "500",
                        msg: kaValue.login.msg.getPhonenVerCodeMsg1 //请输入正确的手机号
                    }
                    kaMsg.error(option);
                    return false;
                }
            } else {
                var option = {
                    code: "500",
                    msg: kaValue.login.msg.getPhonenVerCodeMsg2 //请填写手机号码
                }
                kaMsg.error(option);
                return false;
            }
            login.sliderVerify();
            if (sliderVerifyBoolean) {
                sliderVerifyBoolean = false;
                login.PhonenVerCodeButtonFun(1);
                var tenant = $("#tenant").val();
                var option = kaValue.getUrl(kaValue.login.url.getPhonenVerCode);
                option.data = {
                    "phonenumber": phonenumber,
                    "type": 1,
                    "tenant": tenant
                };
                option.success = function (re) {
                    if (re.code == 200) {
                        kaMsg.success(re);
                    } else {
                        kaMsg.error(re);
                    }
                }
                ka.ajax(option);
            }
        }
    },
    PhonenVerCodeButtonFun: function (types) {// 获取验证按钮
        if (types == 1) {
            var PhonenVerCodeButtonNumberStr = sessionStorage.getItem("PhonenVerCodeButtonNumber");
            if (PhonenVerCodeButtonNumberStr == undefined || PhonenVerCodeButtonNumberStr == null || PhonenVerCodeButtonNumberStr == "") {
                PhonenVerCodeButtonNumberStr = "60";
            }
            var PhonenVerCodeButtonNumber = parseInt(PhonenVerCodeButtonNumberStr);
            if (PhonenVerCodeButtonNumber > 0) {
                $("#PhonenVerCodeButton").html(PhonenVerCodeButtonNumber);
                $("#PhonenVerCodeButton").addClass("layui-btn-disabled");
                $("#PhonenVerCodeButton").prop('disabled', true); // 禁用按钮
                setTimeout(function () {
                    sessionStorage.setItem("PhonenVerCodeButtonNumber", (PhonenVerCodeButtonNumber - 1));
                    login.PhonenVerCodeButtonFun(1);
                }, 1000);
            } else {
                $("#PhonenVerCodeButton").html("获取");
                $("#PhonenVerCodeButton").removeClass("layui-btn-disabled");
                $("#PhonenVerCodeButton").removeAttr('disabled');
                sessionStorage.removeItem("PhonenVerCodeButtonNumber");
            }
        } else {
            var PhonenVerCodeButtonNumberStr = sessionStorage.getItem("PhonenVerCodeButtonNumber");
            if (!(PhonenVerCodeButtonNumberStr == undefined || PhonenVerCodeButtonNumberStr == null || PhonenVerCodeButtonNumberStr == "")) {
                var PhonenVerCodeButtonNumber = parseInt(PhonenVerCodeButtonNumberStr);
                if (PhonenVerCodeButtonNumber > 0) {
                    $("#PhonenVerCodeButton").html(PhonenVerCodeButtonNumber);
                    $("#PhonenVerCodeButton").addClass("layui-btn-disabled");
                    $("#PhonenVerCodeButton").prop('disabled', true); // 禁用按钮
                    setTimeout(function () {
                        sessionStorage.setItem("PhonenVerCodeButtonNumber", (PhonenVerCodeButtonNumber - 1));
                        login.PhonenVerCodeButtonFun(1);
                    }, 1000);
                }
            }
        }
    },
    getLoginQrcode: function () { // 获取二维码
        setTimeout(function () {
            var tenantIds = $("#tenant").val();
            if (tenantIds != undefined && tenantIds != null && tenantIds != "") {
                var uuid = ka.uuidStr()
                var loginQrcodeHtml = `
                    <div style="float: left;text-align: left;width: 100%;">
                        <img src="../static/images/saomalogin.jpg" style="width: 160px;"/>
                        <div style="width: 100%;margin-top: 15px;margin-bottom: 15px;text-align: center;">
                            <a onclick="login.openSaoMaApp()" class="openSaoMaApp">打开<strong class="svelte-16d12rs">KA.APP</strong>扫一扫登录</a>
                        </div>
                    </div>
                    <div style="width: 190px;margin-left: 168px;position: absolute;">
                        <input type="hidden" id="saoMaQrcodeCode" name="saoMaQrcodeCode" placeholder="saoMaQrcodeCode" autocomplete="off" class="layui-input">
                        <div class="tl svelte-x4dxpn"></div>
                        <div class="tr svelte-x4dxpn"></div>
                        <div class="bl svelte-x4dxpn"></div>
                        <div class="br svelte-x4dxpn"></div>
                        <div id="qrcode" uuid="` + uuid + `"></div>
                    </div>
                `;
                $("#loginQrcode").html(loginQrcodeHtml);
                login.createAndverifyQrcode(0);
            } else {
                login.getLoginQrcode();
            }
        }, 100);

    },
    createAndverifyQrcode: function (num) { // 创建验证码二维码
        if (loginmode != 2) {
            return;
        }
        num++;
        //创建
        var uuidStr = $("#qrcode").attr("uuid");
        $("#saoMaQrcodeCode").val(uuidStr);
        var tenant = $("#tenant").val();
        if (num == 1) {
            var option = kaValue.getUrl(kaValue.login.url.loginQrcodeVer);
            option.noloading = false;
            option.data = {
                "code": uuidStr,
                "tenant": tenant,
                "num": num
            };
            option.success = function (re) {
                if (re.code == 200) {
                    var uuid = $("#qrcode").attr("uuid");
                    $("#qrcode").qrcode({
                        render: "canvas",    //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好
                        text: uuid,    //扫描二维码后显示的内容,可以直接填一个网址，扫描二维码后自动跳向该链接
                        width: "164",               //二维码的宽度
                        height: "164",              //二维码的高度
                        background: "#ffffff",       //二维码的后景色
                        foreground: "#000000",        //二维码的前景色
                        src: '../admin/images/ka.png'             //二维码中间的图片
                    });
                    login.createAndverifyQrcode(num);
                } else if (re.code == "501000018") {//二维码已过期
                    var html = `
                    <div class="errorSaoMao">
                        <div class="divOne">
                            <i class="icon iconfont icon-caozuoshibai2"></i>
                        </div>
                        <div class="divTwo">` + kaValue.login.msg.createAndverifyQrcodeMsg + `</div>
                        <div class="divThree">
                            <a href="javascript:void(0);" onclick="login.getLoginQrcode()">点击刷新</a>
                        </div>
                    </div>`;
                    $("#qrcode").prepend(html);
                } else {
                    kaMsg.error({ "code": re.code, "msg": re.msg });
                }
            }
            ka.ajax(option);
        } else {
            var option = kaValue.getUrl(kaValue.login.url.loginQrcodeVer);
            option.noloading = false;
            option.data = {
                "code": uuidStr,
                "tenant": tenant,
                "num": num
            };
            option.success = function (re) {
                if (re.code == 200) {
                    var data = re.data;
                    if (data.status == 0) {
                        login.createAndverifyQrcode(num);
                    } else if (data.status == 2) { // 已经扫码
                        var html = `
                        <div class="successSaoMao">
                            <div class="divOne">
                                <i class="icon iconfont icon-duigou"></i>
                            </div>
                            <div class="divTwo">` + kaValue.login.msg.createAndverifyQrcodeMsg2 + `</div>
                        </div>`;
                        $("#qrcode").prepend(html);
                        $("#loginLaySubmit").removeClass("layui-btn-disabled");
                        $("#loginLaySubmit").removeAttr('disabled');
                    }
                } else if (re.code == "501000018") {//二维码已过期
                    var html = `
                    <div class="errorSaoMao">
                        <div class="divOne">
                            <i class="icon iconfont icon-caozuoshibai2"></i>
                        </div>
                        <div class="divTwo">` + kaValue.login.msg.createAndverifyQrcodeMsg3 + `</div>
                        <div class="divThree">
                            <a href="javascript:void(0);" onclick="login.getLoginQrcode()">点击刷新</a>
                        </div>
                    </div>`;
                    var uuid = $("#qrcode").attr("uuid");

                    $("#qrcode").prepend(html);
                } else {
                    kaMsg.error({ "code": re.code, "msg": re.msg });
                }
            }
            ka.ajax(option);
        }
    },
    openSaoMaApp: function () { //打开二维码
        layer.open({
            type: 1,
            title: false,
            area: ['260px', '260px'], // 宽高
            shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
            content: `<div style="width: 95%;padding-top: 10px;margin: 0 auto;"><div id="qrcodeOpenSaoMaApp"></div></div>`,
            success: function (layero, index, that) {
                $("#qrcodeOpenSaoMaApp").qrcode({
                    render: "canvas",    //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好
                    text: "https://www.baidu.com/",    //扫描二维码后显示的内容,可以直接填一个网址，扫描二维码后自动跳向该链接
                    width: "230",               //二维码的宽度
                    height: "230",              //二维码的高度
                    background: "#ffffff",       //二维码的后景色
                    foreground: "#000000",        //二维码的前景色
                    src: '../admin/images/ka.png'             //二维码中间的图片
                });
            }
        });
    }
}
layui.use(kaValue.layuiUse, function () {
    //获取头部账号 和 头部URL
    login.PhonenVerCodeButtonFun(0);
    //系统版本
    login.getConfig("sys.login.version", function (re) {
        var configValue = re.configValue;
        $("#kaTitleVersion").html(configValue);
    })
    //登录标题
    login.getConfig("sys.login.title", function (re) {
        var configValue = re.configValue;
        $("#loginTitle").html(configValue);
    })
    //登录小标题
    login.getConfig("sys.login.smatitle", function (re) {
        var configValue = re.configValue;
        $("#smatitle").html(configValue);
    });
    //注册账号
    login.getConfig("sys.account.registerUser", function (re) {
        var configValue = re.configValue;
        if (configValue == "true") {
            $("#registerAccount").click(function () {
                login.register();
            });
        } else {
            $("#registerAccountDiv").hide();
        }
    });
    //账号自助-验证码开关
    login.getConfig("sys.account.captchaEnabled", function (re) {
        var configValue = re.configValue;
        if (configValue == "true") {
            login.captchaImage();
        }
    });
    //加载租户
    login.loginTenant(function (re) {
        var json = re.data;
        var html = ``;
        for (var j in json) {
            if (j == 0) {
                var currTenantId = json[j].id;
                var currTenantName = json[j].name;
                $("#forgotPasswordX").attr("href", "forgotPassword.html?currTenantId=" + encodeURI(encodeURIComponent(currTenantId)) + "&currTenantName=" + encodeURI(encodeURIComponent(currTenantName)));
            }
            html = html + `<option value="` + json[j].id + `">` + json[j].name + `</option>`;
        }
        $("#tenant").html(html);
        layui.form.render('select');
        layui.form.on('select(tenant)', function (data) {
            var elem = data.elem; // 获得 select 原始 DOM 对象
            var value = data.value; // 获得被选中的值
            var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
            for (var i in elem) {
                var valueStr = $(elem[i]).attr("value");
                if (valueStr == value) {
                    var textStr = $(elem[i]).text();
                    $("#forgotPasswordX").attr("href", "forgotPassword.html?currTenantId=" + encodeURI(encodeURIComponent(value)) + "&currTenantName=" + encodeURI(encodeURIComponent(textStr)));
                    break;
                }
            }
            layer.msg(this.innerHTML + ' 的 value: ' + value); // this 为当前选中 <option> 元素对象
        });
    });
    ka.watermark('body');
    layui.form.on('select(language-select-filter)', function (data) {
        var value = data.value; // 获得被选中的值
        sessionStorage.setItem("kaLanguageTypeCurrent", value)
        kaLanguage.loadingValue(value);
        window.location.reload();
    });
    //
    layui.form.on('checkbox(automatic-logon-checkbox)', function (data) {
        var elem = data.elem; // 获得 radio 原始 DOM 对象
        layer.msg('记住状态: ' + elem.checked);
    });
    //其他登录
    $("#otherLogin").click(function () { // 请选择登录方式
        layer.alert(kaValue.login.msg.otherLogin, {
            title: false,
            btn: kaValue.login.otherLoginTab, //['手机登录', '企业微信', '钉钉登录']
            btnAlign: 'c', // 按钮居中显示
            btn1: function () {

            },
            btn2: function () {

            },
            btn3: function () {

            }
        });
    });
    $("#registerAccount").click(function () { // 请选择注册身份
        layer.alert(kaValue.login.msg.registerAccount, {
            title: false,
            btn: kaValue.login.registerAccountTab, // ['供应商', '经销商', '系统员工']
            btnAlign: 'c', // 按钮居中显示
            btn1: function () {

            },
            btn2: function () {

            },
            btn3: function () {

            }
        });
    });
    // 登 录 提 交
    var formOption = {};
    formOption.id = 'submit(login)';
    //验证参数
    formOption.kaver = function (field) {
        if (field.tenant == undefined || field.tenant == null || field.tenant == "") {
            kaMsg.error({ "msg": kaValue.login.msg.formOption1, "code": "500" });//租户不能为空！
            return false;
        }
        if (loginmode == 0) {
            if (field.username == undefined || field.username == null || field.username == "") {
                kaMsg.error({ "msg": kaValue.login.msg.formOption2, "code": "500" });//账户不能为空！
                return false;
            }
            if (field.password == undefined || field.password == null || field.password == "") {
                kaMsg.error({ "msg": kaValue.login.msg.formOption3, "code": "500" });//密码不能为空！
                return false;
            }
        } else if (loginmode == 1) {
            if (field.phonenumber == undefined || field.phonenumber == null || field.phonenumber == "") {
                kaMsg.error({ "msg": kaValue.login.msg.formOption4, "code": "500" });//手机号码不能为空！
                return false;
            }
            if (!(kaValue.regMobile.test(field.phonenumber))) {
                kaMsg.error({ "code": "500", "msg": kaValue.login.msg.formOption5});//手机号码不正确！
                return false;
            }
            if (field.codePhonenumber == undefined || field.codePhonenumber == null || field.codePhonenumber == "") {
                kaMsg.error({ "msg": kaValue.login.msg.formOption6, "code": "500" });//手机验证码不能为空！
                return false;
            }
        } else if (loginmode == 2) {

        }
        if (login.verificationImgBoolean) {
            if (field.code == undefined || field.code == null || field.code == "") {
                kaMsg.error({ "msg": kaValue.login.msg.formOption7, "code": "500" });//验证码不能为空！
                return false;
            }
        }
    }
    formOption.cb = function (field) {
        //清楚 Cookie
        kaBuss.kaCookieClear2();
        var appPhone = ka.isAppPhoneOrExe();
        field.clientType = appPhone.type;
        field.loginmode = loginmode;
        if (loginmode == 2) {
            field.saoMaQrcodeCode = $("#saoMaQrcodeCode").val();
        }
        login.login(field, function (re) {
            if (re.automaticLogon != undefined && field.automaticLogon != "" && field.automaticLogon == "1") {
                login.setAutomaticLogon(re.LoginBody);
            }
            //登录成功后初始化系统信息
            var tokenAuth = re.token;
            var kaLoginDataParam = re.kaLoginDataParam;
            kaToken.set(tokenAuth);
            kaCurrUser.set(kaLoginDataParam);
            document.cookie = "Authorization=" + tokenAuth + "; path=/";
            kaCookie.set("username", kaLoginDataParam.userName, 30, "/", location.hostname);
            kaCookie.set("tenant", kaLoginDataParam.tenant, 30, "/", location.hostname);
            kaCookie.set("phonenumber", kaLoginDataParam.phonenumber, 30, "/", location.phonenumber);
            kaCookie.set("rememberMe", "true", 30, "/", location.hostname);
            kaCookie.set("password", kaLoginDataParam.password, 30, "/", location.hostname);
            kaCookie.set("Authorization", tokenAuth, 2, "/", location.hostname);
            var kts = kaLoginDataParam.userName[0].toUpperCase() + kaLoginDataParam.userName.slice(1);
            kaCookie.set("Admin-Token", tokenAuth, 2, "/", location.hostname);
            kaCookie.set("tenantId", "default", 30, "/", location.hostname);
            // 登录信息
            var urlindexpage = $.getUrlParam("indexpage");
            if (urlindexpage != undefined && urlindexpage != null && urlindexpage != "") {
                sessionStorage.setItem("indexpage", urlindexpage);
                location.href = decodeURI(decodeURIComponent(urlindexpage))
            } else {
                //登录本地Html
                var kaindexpagestr = sessionStorage.getItem("indexpage");
                if (kaindexpagestr != undefined && kaindexpagestr != null && kaindexpagestr != "") {
                    location.href = kaindexpagestr + ".html"
                } else {
                    kaBuss.locationHomeHref()
                }
            }
        });
    }
    kaForm.init(formOption);
    login.init();
})

